/* Copyright (C) 2021 Rain */

/* This file is part of XNIX. */

/* 
  XNIX is free software: you can redistribute it and/or modify 
  it under the terms of the GNU General Public License as published by 
  the Free Software Foundation, either version 3 of the License, or 
  (at your option) and later version. 
*/

/*
  XNIX is distributed in the hope that it will be useful, 
  but WITHOUT ANY WARRANTY; without even the implied warranty of 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
  GNU General Public License for more details. 
*/

/*
  You should have received a copy of the GNU General Public License 
   along with XNIX. If not, see <https://www.gnu.org/licenses/>.  
*/




#include <kernel/init.h>
#include <kernel/errno.h>
#include <kernel/types.h>

#include <kernel/console.h>
#include <kernel/mm.h>

#include <kernel/int.h>
#include <kernel/panic.h>

#include <arch/io.h>
#include <modules/beep.h>


static void beep_on(void) {
	__uint8_t a;

	a = (inb(0x61) | 0x03) & 0x0f;
	outb(a, 0x61);

	return;
}

void beep_off(void) {
	__uint8_t a;

	a = inb(0x61) & 0x0d;
	outb(a, 0x61);

	return;
}


errno_t beep_init(void) {
	return 0;
}

void beep(__uint32_t value) {
	value = 1193180000 / value;

	outb(0xb6, 0x43);
	outb(value & 0xff, 0x42);
	outb((value >> 8) & 0xff, 0x42);

	beep_on();

	return;
}

